<script type="text/javascript">
    function setData(targetElement, targetElmentValue, targetElementLabel, targetElementLabelValue, myDialog){
        $(myDialog).dialog("close");
        document.getElementById(targetElement).value = targetElmentValue;
        document.getElementById(targetElementLabel).value = targetElementLabelValue;
        
        if($('#'+targetElement).val() != ''){
            $('#btn_simpan').removeAttr('disabled');
        }else{
            $('#btn_simpan').attr('disabled')=='true';
        }
        
        var hdnCon = $('#hdnControllerName').val();
        
        for(var i = 0; i < hdnCon.split('|').length; i++){
            var con = hdnCon.split('|')[i];
            $('#'+con+'_select').removeAttr('disabled');
            $('#'+con+'_select').removeAttr('checked');
        }
        
        $.ajax ({
        type:"POST",
        url:"<?php echo Yii::app()->createUrl('/specialajax/checklist'); ?>",
        data:"menuGroupID="+targetElmentValue,
        dataType:"json",
            success:function(data){        
                var xStr = $('#hdnCheckBox').val();
                    
                for(var r = 0; r < xStr.split("|").length; r++){
                    $('#'+xStr.split("|")[r]).removeAttr('checked');
                }
                
                for(var i = 0; i < data.length; i++){
                    var arrRV = data[i].RuleValues;
                    var cn = data[i].ControllerName;
                    
                    for (var c = 0; c < arrRV.split(',').length; c++ ){
                        var rv = arrRV.split(',')[c];
                        rv = rv.replace("'", "");
                        rv = rv.replace("'", "");
                        
                        $('#'+cn+'_'+rv).attr('checked','checked');
                    }
                }
            }
        });   
    }
    
    function setCheckbox(checkBoxElm){
        var xStr = $('#hdnCheckBox').val();
        
        for(var r = 0; r < xStr.split("|").length; r++){
            var elm = xStr.split("|")[r];
            if($('#'+elm).attr('disabled')=='disabled')
                continue;
            
            if (elm.split('_')[0] == checkBoxElm.split('_')[0]){
                if ($('#'+checkBoxElm).attr('checked')=='checked'){
                    $('#'+elm).attr('checked','checked');
                }else{
                    $('#'+elm).removeAttr('checked');
                }
            }
        }
    }
</script>
<?php
Yii::app()->clientScript->registerScript(
'myHideEffect',
'$(".flash-message").animate({opacity: 1.0}, 2000).fadeOut("slow");',CClientScript::POS_READY);

/* @var $this SysappaccessruleController */
/* @var $model Sysappaccessrule */

$this->breadcrumbs=array(
	'Sysappaccessrules'=>array('index'),
	'Create',
);

$this->menu=array(
	array('label'=>'List Sysappaccessrule', 'url'=>array('index')),
	array('label'=>'Manage Sysappaccessrule', 'url'=>array('admin')),
);
?>

<h1>Manage Access Rule</h1>

<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'sysappaccessrule-form',
	// Please note: When you enable ajax validation, make sure the corresponding
	// controller action is handling ajax validation correctly.
	// There is a call to performAjaxValidation() commented in generated controller code.
	// See class documentation of CActiveForm for details on this.
	'enableAjaxValidation'=>false,
)); ?>

<font color="red"><?php echo $form->error($model,'MenuGroupID'); ?></font>

<?php
    foreach(Yii::app()->user->getFlashes() as $key=>$pesan) {
        echo "<div class='flash-$key'>" . $pesan . "</div>\n";
    }
?>
<table style="border-style: solid;border-width: medium;">
    <tr>
        <div class="row">
		<td><?php echo $form->labelEx($model,'MenuGroupID'); ?></td>
                <td colspan="<?php echo count($arrAllAction)+1; ?>">: <?php echo CHtml::activeNumberField($model,'MenuGroupID',array('id' => 'txt_MenuGroupID', 'size'=>11, 'readonly'=>true)); ?> 
                    <?php echo CHtml::Button('...', array('onclick'=>'$("#myDialog_MenuGroupID").dialog("open"); return false;',)); ?> 
                    <?php echo "";echo CHtml::activeTextField($menuGroupModel, 'MenuGroupName',array('style'=>'border :0', 'readOnly'=>"", 'id'=>'lb_MenuGroupID')); ?>
                    
                    <?php $this->beginWidget('zii.widgets.jui.CJuiDialog', array(
                            'id' => 'myDialog_MenuGroupID',
                            'options' => array('title' => 'Sysappaccessrule-MenuGroupID','width' => 600,'height' => 500,'autoOpen' => false,),
                            ));
                            $TableName = 'sysappmenugroup';
                            $ColumnName = 'MenuGroupID';
                            $targetElement = 'txt_MenuGroupID';
                            $targetElementLabel = 'lb_MenuGroupID';
                            $targetValueLabel = 'MenuGroupName';
                            $dialogName = '#myDialog_MenuGroupID';
                            $arrColumns = NULL;
                            $modelProvider = NULL;
                            
                            $this->renderFile(Yii::getPathOfAlias('application.components').'\myDialog.php',
                                array('TableName' =>$TableName,
                                    'ColumnName' =>$ColumnName,
                                    'targetElement' =>$targetElement,
                                    'targetElementLabel' =>$targetElementLabel,
                                    'targetValueLabel' =>$targetValueLabel,
                                    'dialogName' =>$dialogName,
                                    'arrColumns' =>$arrColumns,
                                    'modelProvider'=>$modelProvider)
                              );
                        ?>
                    <?php $this->endWidget('zii.widgets.jui.CJuiDialog'); ?> 
                </td>
	</div>
    </tr>
    <tr>
        <td style="border-top-style: solid;border-top-width: thin;border-bottom-style: solid;border-bottom-width: thin;border-right-style: solid;border-right-width: thin;background-color: #FFF6BF">Controller</td>
        <td style="border-style: solid;border-width: thin;border-left-style: none;border-left-width: -1;background-color: #FFF6BF;"></td>
        <?php foreach ($arrAllAction as $f){
            $fr = str_replace('action', '', $f);  
        ?>
        
            <td style="border-top-style: solid;border-top-width: thin;border-bottom-style: solid;border-bottom-width: thin;background-color: #FFF6BF"><?php echo $fr; ?> </td>
        <?php } ?>
    </tr>
    <?php 
        $allCheckboxElementID = "";
        $allCheckboxControllerName = "";
        foreach (array_keys($controllerList) as $controllerClassName) {
            $controllerName = str_replace("Controller", "", $controllerClassName);
            if(strpos($controllerClassName, 'Controller') ===FALSE)
                continue;
            
            $allCheckboxControllerName .= $controllerName.'|';
            
            if (is_int(array_search($controllerName, $arrExceptedController)))
                continue;
    ?>      
        <tr>
            <td style="border-right-style: solid;border-right-width: thin;"><?php echo $controllerName; ?></td>
            <td style="border-right-style: solid;border-right-width: thin;"><?php echo CHtml::checkBox($controllerName.'_select', FALSE, array('onClick'=>"setCheckbox('{$controllerName}_select')",'disabled'=>empty($allActionChecked)?'disabled':'')); ?></td>
            <?php
            foreach ($arrAllAction as $f){
                $fr = str_replace('action', '', $f);
                $allCheckboxElementID .= $controllerName.'_'.strtolower($fr)."|";
            ?>    
            
            <td>
                <?php if (!empty($allActionChecked[$controllerName])){ ?>
                    <?php $cbDis = is_int(array_search($f, $controllerList[$controllerClassName]));?>
                    <?php echo CHtml::checkBox($controllerName.'_'.strtolower($fr), is_int(array_search(strtolower("'".$fr."'"), $allActionChecked[$controllerName]))?TRUE:FALSE, array('disabled'=>!$cbDis)); ?>  
                <?php } else { ?>
                    <?php $cbDis = is_int(array_search($f, $controllerList[$controllerClassName]));?>
                    <?php echo CHtml::checkBox($controllerName.'_'.strtolower($fr), FALSE, array('disabled'=>!$cbDis)); ?>  
                <?php }?>
            </td>
            <?php } ?>
        </tr>    
    <?php } ?>
        <tr>
            <td colspan="<?php echo count($arrAllAction)+2;?>" style="border-top-style: solid;border-top-width: thin;">
                <?php echo CHtml::submitButton('Save',array('id'=>'btn_simpan', 'disabled'=>'true')); ?>
            </td>
        </tr>
</table>
<?php $this->endWidget(); ?>
<?php echo "<input type='hidden' id='hdnCheckBox' value='{$allCheckboxElementID}'>"; ?>
<?php echo "<input type='hidden' id='hdnControllerName' value='{$allCheckboxControllerName}'>"; ?>